1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 module hip.hiprenderer.texture;
12 import hip.error.handler;
13 import hip.hiprenderer.renderer;
14 import hip.math.rect;
15 import hip.image;
16 public import hip.util.data_structures:Array2D;
17 public import hip.api.renderer.texture;
18 
19 class HipTexture : IHipTexture
20 {
21     IImage img;
22     int width,height;
23     TextureFilter min, mag;
24     private __gshared HipTexture pixelTexture;
25 
26     bool hasSuccessfullyLoaded(){return img.getWidth > 0;}
27     public static immutable(HipTexture) getPixelTexture()
28     {
29         if(pixelTexture is null)
30         {
31             pixelTexture = new HipTexture();
32             pixelTexture.img = cast(IImage)HipImageImpl.getPixelImage();
33             pixelTexture.textureImpl.load(pixelTexture.img);
34         }
35         return cast(immutable)pixelTexture;
36     }
37 
38     /**
39     *   Make it available for implementors
40     */
41     package IHipTexture textureImpl;
42     /**
43     *   Initializes with the current renderer type
44     */
45     protected this(){textureImpl = HipRenderer.getTextureImplementation();}
46 
47     this(IImage image)
48     {
49         this();
50         if(image !is null)
51             load(image);
52     }
53 
54     public IHipTexture getBackendHandle(){return textureImpl;}
55 
56     ///Binds texture to the specific slot
57     public void bind(int slot = 0)
58     {
59         textureImpl.bind(slot);
60     }
61     public void unbind(int slot = 0)
62     {
63         textureImpl.unbind(slot);
64     }
65     public void setWrapMode(TextureWrapMode mode){textureImpl.setWrapMode(mode);}
66     public void setTextureFilter(TextureFilter min, TextureFilter mag)
67     {
68         this.min = min;
69         this.mag = mag;
70         textureImpl.setTextureFilter(min, mag);
71     }
72     
73     Rect getBounds(){return Rect(0,0,width,height);}
74 
75     /**
76     *   Returns whether the load was successful
77     */
78     protected bool loadImpl(in IImage img)
79     {
80         import hip.console.log;
81         this.img = cast(IImage)img; //Promise it won't modify
82         this.width = img.getWidth;
83         this.height = img.getHeight;
84         hiplog("Uploading Texture[",img.getName,"]", img.getWidth, "x", img.getHeight);
85         this.textureImpl.load(img);
86         setTextureFilter(TextureFilter.NEAREST, TextureFilter.NEAREST);
87         return width != 0;
88     }
89     int getWidth() const {return width;}
90     int getHeight() const {return height;}
91 }